package com.me.screen;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.me.constant.Constant;
import com.me.enums.ScreenEnum;
import com.me.happybugs.HappyBugs_Main;

public class EndGame extends BaseScreen {

	Image imgBackGround;
	TextButton btn_Replay;
	TextButton btn_shareScore;
	TextButton btn_shareGame;
	Label lb_yourScore;
	private Image img_bgTop;
	private ImageButton img_back;

	@Override
	public void show() {
		super.show();

		BaseScreen.stage.clear();

		imgBackGround = new Image(HappyBugs_Main.getAtlas().findRegion(
				"datagame/woodBG"));

		btn_Replay = new TextButton("Replay", Constant.skin,
				"textButton_45_bold");
		btn_Replay.setX(440);
		btn_Replay.setY(450);
		btn_Replay.setWidth(450);
		btn_Replay.setHeight(100);
		btn_Replay.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				Constant.resetGame();
				Constant.soundManager.resetAllSound();
				HappyBugs_Main.getScreenManager().createScreen(
						ScreenEnum.PLAY_SCREEN);
				HappyBugs_Main.getScreenManager().getScreen().show();
			}

		});

		img_bgTop = new Image(
				Constant.atlas.findRegion("datagame/menu-bottom-thanh-ngang"));
		img_bgTop.setBounds(0, Constant.STAGE_HEIGHT - 70,
				Constant.STAGE_WIDTH, 70);

		TextureRegionDrawable bup = new TextureRegionDrawable(
				Constant.atlas.findRegion("datagame/Back_up"));
		TextureRegionDrawable bdown = new TextureRegionDrawable(
				Constant.atlas.findRegion("datagame/Back_down"));
		img_back = new ImageButton(bup, bdown);
		img_back.setX(20);
		img_back.setY(Constant.STAGE_HEIGHT - img_bgTop.getHeight()
				+ (img_bgTop.getHeight() - img_back.getHeight()) / 2);
		img_back.addListener(new ClickListener() {
			@Override
			public void touchUp(InputEvent event, float x, float y,
					int pointer, int button) {
				super.touchUp(event, x, y, pointer, button);
				HappyBugs_Main.getScreenManager().createScreen(
						ScreenEnum.MENU_SCREEN);
				HappyBugs_Main.getScreenManager().getScreen().show();
			}
		});

		btn_shareScore = new TextButton("Share score", Constant.skin,
				"textButton_45_bold");
		btn_shareScore.setX(440);
		btn_shareScore.setY(320);
		btn_shareScore.setWidth(450);
		btn_shareScore.setHeight(100);
		btn_shareScore.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				Constant.b_bestScoreMain = false;
				HappyBugs_Main.getScreenManager().createScreen(
						ScreenEnum.HIGH_SCORE_SCREEN);
				HappyBugs_Main.getScreenManager().getScreen().show();
			}
		});

		btn_shareGame = new TextButton("Share game", Constant.skin,
				"textButton_45_bold");
		btn_shareGame.setX(440);
		btn_shareGame.setY(200);
		btn_shareGame.setWidth(450);
		btn_shareGame.setHeight(100);
		btn_shareGame.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				HappyBugs_Main.getApi().showToast("Please wait!");
				String str = "https://play.google.com/store/apps/developer?id=gameStudio";
				HappyBugs_Main.getApi().shareTextUrl(str);
			}

		});

		lb_yourScore = new Label("Your score: " + Constant.iScore+" ",
				Constant.skin, "label32");
		lb_yourScore.setX(Constant.STAGE_WIDTH - lb_yourScore.getWidth()-10);
		lb_yourScore.setY(20);

		stage.addActor(imgBackGround);
		stage.addActor(img_bgTop);
		stage.addActor(img_back);
		stage.addActor(btn_Replay);
		stage.addActor(btn_shareScore);
		stage.addActor(btn_shareGame);
		stage.addActor(lb_yourScore);
	}

	@Override
	public void render(float arg0) {
		Gdx.gl.glClearColor(0, 0, 0.2f, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

		// tell the camera to update its matrices.
		camera.update();

		// tell the SpriteBatch to render in the coordinate system specified by
		// the camera.
		sb.setProjectionMatrix(camera.combined);

		// begin a new batch and draw the bucket and all drops
		sb.begin();
		sb.end();

		if (Gdx.input.isKeyPressed(Keys.BACK)) {
			HappyBugs_Main.getScreenManager().createScreen(
					ScreenEnum.MENU_SCREEN);
			HappyBugs_Main.getScreenManager().getScreen().show();
		}
		stage.setViewport(Constant.STAGE_WIDTH, Constant.STAGE_HEIGHT);
	}

	@Override
	public void dispose() {
		super.dispose();
	}

}
